<template>
	<view class="gold-tab">
		<u-sticky>
			<u-tabs :list="list" :is-scroll="false" active-color="#000" :current="current" @change="change"></u-tabs>
		</u-sticky>

		<view>
			<view class="list">
				<my-coupon :type="i.send.coupon_type.type_class" :amount="i.send.amount" :threshold="i.send.threshold" :expire="i.expire_at"
				 :btn="i.sell_state==0?'取消售卖':'已售出'" v-for="i in list[current].data" :key="i.id" @click="clickHandle(i)"></my-coupon>
			</view>

			<u-loadmore :status="list[current].status" bg-color="#EFF1F4" margin-top="30" margin-bottom="30"></u-loadmore>
		</view>

		<u-modal v-model="cancelModal" show-cancel-button content="确认取消售卖？" @confirm="cancel"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [{
					name: '全部',
					type: 3,
					page: 1,
					status: 'loadmore',
					data: []
				}, {
					name: '出售中',
					type: 0,
					page: 1,
					status: 'loadmore',
					data: []
				}, {
					name: '已售出',
					type: 1,
					page: 1,
					status: 'c',
					data: []
				}],
				current: 0,
				cancelModal: false,
				cancelId: 0
			}
		},
		onShow() {
			this.getData(true)
		},
		onReachBottom() {
			this.getData()
		},
		methods: {
			change(index) {
				this.current = index;
				this.getData(true)
			},
			getData(clear = false) {
				let current = this.list[this.current]
				if (clear) {
					current.data = []
					current.status = 'loadmore'
				}
				if (current.status == 'nomore') return;

				current.status = 'loading'
				let params = {
					type: current.type,
					page: current.page
				}
				this.$u.get('/coupons/sale/record', params).then(res => {
					let coupon = res.data.result.record
					let {
						last_page,
						data
					} = coupon

					current.data = current.data.concat(coupon.data)
					if (current.page == last_page) {
						current.status = 'nomore'
					} else {
						current.status = 'loadmore'
						current.page++
					}
				})
			},
			clickHandle(coupon) {
				if (coupon.sell_state == 0) {
					this.cancelModal = true
					this.cancelId = coupon.id
				}
			},
			cancel() {
				this.$u.post('coupons/sale/cancel?id=' + this.cancelId).then(res => {
					this.$msg(res.data.msg)
					this.getData(true)
				})
			}
		}
	}
</script>

<style lang="scss">

</style>
